<?php
if (!defined('APP') or !constant('APP')) die('Direct access not allowed!');


class UserProfileModel 
{
    private static $db = 'users_profile';
    
    public static function getUserProfile($id_user)
    {
        $query = "SELECT * FROM `users_profile` WHERE id_user = :id_user;";
        $result = DB::getPDO()->prepare($query);
        $result->execute(array(
            ':id_user' => intval($id_user)           
            ));  
        $result = $result->fetch();
        
        if (sizeof($result) < 1) {         
            return false;
        }
        return $result;
    }
    
    public static function insertOrUpdate($data)
    {
        if (!isset($data['id_user'])) 
        {
            die('Not ID USER. SHOCK!!!');
            return false;
        }
       
        // sprawdzenie czy użytkownik posiada profil
        $query = "SELECT id FROM `users_profile` WHERE id_user = :id_user;";
        $result = DB::getPDO()->prepare($query);
        $result->execute(array(
            ':id_user' => intval($data['id_user'])           
            ));  
        $result = $result->fetchAll();
        
        if (sizeof($result) < 1) {         
            $query = "INSERT INTO `users_profile` (`about`, `sex`, `gg`, `id_user`, `firstname`, `lastname`, `nick`, `skype`, `website` )
                        VALUES (:about, :sex, :gg, :id_user, :firstname , :lastname , :nick, :skype, :website);";
        }
        else {
            $query = "UPDATE `users_profile` SET `about`=:about, `sex`=:sex, `gg`=:gg, `firstname`=:firstname, `lastname`=:lastname, `nick`=:nick, `skype`=:skype, `website`=:website WHERE `id_user`=:id_user";   
        }
        
        $d = array();
        if (isset($data['firstname'])) $d[':firstname'] = htmlspecialchars($data['firstname']); else $d[':firstname'] = "";
        if (isset($data['lastname'])) $d[':lastname'] = htmlspecialchars($data['lastname']); else $d[':lastname'] = "";
        if (isset($data['nick'])) $d[':nick'] = htmlspecialchars($data['nick']); else $d[':nick'] = "";
        if (isset($data['skype'])) $d[':skype'] = htmlspecialchars($data['skype']); else $d[':skype'] = "";
        if (isset($data['website'])) $d[':website'] = htmlspecialchars($data['website']); else $d[':website'] = "";
        if (isset($data['sex'])) $d[':sex'] = intval($data['sex']); else $d[':sex'] = 0;
        if (isset($data['gg'])) $d[':gg'] = htmlspecialchars($data['gg']); else $d[':gg'] = "";
        if (isset($data['about'])) $d[':about'] = htmlspecialchars($data['about']); else $d[':about'] = "";
        
        $d[':id_user'] = intval($data['id_user']);
            
        $result = DB::getPDO()->prepare($query);
        $r = $result->execute($d);   
     
        return $r;
    }    
}

?>
